 <template>
    <div>
        <div id="toAvatar" @click="dialogUploadAvVisible = true">
            <div>修改头像</div>
        </div>
    <el-dialog :visible.sync="dialogUploadAvVisible" title="头像修改">
      <el-form :model="load" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="上传" prop="" required>
          <el-upload
            ref="PhotoUrl"
            :file-list="field101fileList"
            :action="field101Action"
            :before-upload="field101BeforeUpload"
            :on-success="successload"
            list-type="picture-card"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary" @click="createload">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


    </div>
</template>

<script>
    export default {
        name: "changeAv",
        components:{

        },
        data(){
            return {
                dialogUploadAvVisible:false,
                dialogImageUrl: '',
                dialogVisible: false,
                field101Action:"https://localhost:44347/Upload",
                field101fileList: [],
                dto:{
                    id:"",
                    userImg:""
                } 
            }
        },
        mounted(){
            this.dto.id=localStorage.getItem('id');
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            field101BeforeUpload(file) {
                let isRightSize = file.size / 1024 / 1024 < 2;
                if (!isRightSize) {
                    this.$message.error("文件大小超过 2MB");
                }
                return isRightSize;
            },
            successload(r,file) {
                this.dto.userImg = file.response;
            },
            createload()
            {
                this.axios.post("https://localhost:44347/UpImg",this.dto)
                    .then((response)=>{
                        if(response.data>0)
                        {
                            alert('修改成功!');
                            //location. reload()    页面刷新
                            this.$router.go(0);      //页面刷新
                            localStorage.setItem('img',this.dto.userImg);
                            this.ialogUploadAvVisible=false;                 
                        }
                        else
                        {
                            alert('修改失败!');
                        }
                    })
            }
        }
    }
</script>